package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.UsersFriend;
import com.example.demo.entity.dto.MyFriendsAddInfoDTO;
import com.example.demo.entity.vo.FriendReviewVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IUsersFriendService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 好友关系 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2024-03-13
 */
@Api(tags = "好友关系")
@RestController
@RequestMapping("/users-friend")
public class UsersFriendController {

    @Resource
    private IUsersFriendService usersFriendService;


    /**
     * 查询我的好友
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "查询我的好友")
    @SaCheckLogin
    @GetMapping("/getMyFriends")
    public Result<List<MyFriendsAddInfoDTO>> getMyFriends(){
        //通过token获取自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        List<MyFriendsAddInfoDTO> myFriends = usersFriendService.getMyFriends(loginIdAsInt);
        return Result.success(myFriends).msg("好友列表");
    }





    /**
     * 好友审核
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "好友审核")
    @SaCheckLogin
    @PostMapping("/friendReview")
    public Result friendReview(@RequestBody FriendReviewVO friendReviewVO){
        //我同意他的好友请求
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(friendReviewVO,usersFriend);
        usersFriend.setFriendId(loginIdAsInt);
        usersFriendService.updateById(usersFriend);
        //我同意添加对方后，我也应该是对方的好友
        UsersFriend usersFriend1 = new UsersFriend();
        usersFriend1.setUserId(loginIdAsInt);
        //user_friend 的id查询这个人的user_id
        Integer userId = usersFriendService.getById(friendReviewVO.getId()).getUserId();
        usersFriend1.setFriendId(userId);
        usersFriend1.setStatus(1);
        usersFriendService.save(usersFriend1);
        return Result.success().msg("操作成功");
    }



    /**
     * 查询谁要添加我为好友
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "查询谁要添加我为好友")
    @SaCheckLogin
    @GetMapping("/getMyFriendsAddInfo")
    public Result getMyFriendsAddInfo(){
        //通过token获取自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        List<MyFriendsAddInfoDTO> myFriendsAddInfo = usersFriendService.getMyFriendsAddInfo(loginIdAsInt);
        return Result.success(myFriendsAddInfo);
    }




    /**
     * 添加某人为好友，传入被添加人id
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "添加某人为好友，传入被添加人id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "被添加人id",required = true)
    })
    @SaCheckLogin
    @PostMapping("/add/{id}")
    public Result<String> add(@PathVariable Integer id) {
        //通过sa-token获取到自己的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();

        if(loginIdAsInt == id){
            return Result.error().msg("不能添加自己为好友");
        }

        UsersFriend usersFriend = new UsersFriend();
        usersFriend.setUserId(loginIdAsInt);
        usersFriend.setFriendId(id);
        boolean b = usersFriendService.addFriend(usersFriend);
        if(!b){
            return Result.error().msg("等待同意或你被拉黑或已是好友");
        }
        return Result.success().msg("添加成功");
    }



    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "查询好友关系所有数据")
    @GetMapping
    public Result<List<UsersFriend>> findAll() {
        return Result.success(usersFriendService.list()).msg("查询成功");
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<UsersFriend> findOne(@PathVariable Integer id) {
        return Result.success(usersFriendService.getById(id)).msg("查询成功");
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",example="1",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",example="10",required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<UsersFriend>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<UsersFriend> queryWrapper = new QueryWrapper<>();
        return Result.success().data(usersFriendService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("查询成功");
    }

    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    //TODO 记得来创建这个VO
    public Result<String> add() {
        //TODO VO转Entity
        //usersFriendService.saveOrUpdate(usersFriend);
        return Result.success().msg("添加成功");
    }

    /**
     * 更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "更新接口")
    @PutMapping
    public Result<String> save(@RequestBody UsersFriend usersFriend) {
        usersFriendService.saveOrUpdate(usersFriend);
        return Result.success().msg("更新成功");
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        usersFriendService.removeById(id);
        return Result.success().msg("删除成功");
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @ApiOperation(value = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        usersFriendService.removeByIds(ids);
        return Result.success().msg("批量删除成功");
    }
}